<template>
  <!-- 已登录 -->
  <div v-if="user" class="userInfo header">
    <!-- 基本信息 -->
    <div class="base">
      <div class="left">
        <van-image fit="cover" src="https://img.yzcdn.cn/vant/cat.jpeg" class="avatar" round/>
        <span class="name">黑马头条</span>
      </div>
      <div class="right">
        <van-button type="default" size="mini" round>编辑资料</van-button>
      </div>
    </div>
    <!-- 粉丝、关注 -->
    <div class="data">
        <div class="data-item">
          <span>0</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>0</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>0</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>0</span>
          <span>获赞</span>
        </div>
    </div>
    <!-- 宫格 -->
    <van-grid class="nav-grid" :column-num="2" clickable >
      <van-grid-item text="收藏">
        <van-icon name="star-o" color="red" size="20px"/>
        <span class="text" style="font-size:13px">收藏</span>
      </van-grid-item>
      <van-grid-item text="文字">
        <van-icon name="clock-o" color="red" size="20px"/>
        <span class="text" style="font-size:13px">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link style="margin-top:5px"/>
    <van-cell title="小智同学" is-link />
    <van-cell class="logout-cell" title="退出" center @click="onLogout"/>
  </div>
  <!-- 未登录 -->
  <div v-else class="header not-login">
      <div class="header">
        <img src="@/assets/sc/mobile.png" alt="" class="not-login-img" @click="$router.push('/login')">
        <span class="not-login-span">登录 / 注册</span>
        <img class="mobile-img" 
        src="@/assets/sc/banner.png" height="200px" width="100%">
      </div>
      <div class="grid-nav">
        <!-- 宫格 -->
        <van-grid class="nav-grid" :column-num="2" clickable>
          <van-grid-item text="收藏">
            <van-icon name="star-o" color="red" size="20px"/>
            <span class="text" style="font-size:13px">收藏</span>
          </van-grid-item>
          <van-grid-item text="文字">
            <van-icon name="clock-o" color="red" size="20px"/>
            <span class="text" style="font-size:13px">历史</span>
          </van-grid-item>
        </van-grid>
      </div>
      <van-cell title="消息通知" is-link url="" style="margin-top: 5px;"/>
      <van-cell title="小智同学" is-link url="" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'my',
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },

  computed: {
    ...mapState(['user'])
  },

  created () {
    // 初始化的时候，如果用户登录了，我才请求获取当前登录用户的信息
    if (this.user) {
      this.loadUser()
    }
  },

  methods: {
   //退出
   onLogout(){
    // 退出提示
    // 在组件中需要使用 this.$dialog 来调用弹框组件
    this.$dialog.confirm({
      title: '确认退出吗？'
    }).then(() => {
      // on confirm
      // 确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
      this.$store.commit('setUser', null)
    }).catch(() => {
      // on cancel
      console.log('取消执行这里')
    })
  },
  async loadUser () {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
      } catch (err) {
        console.log(err)
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
   .header {
    height: 210px;
    background: url("@/assets/sc/banner.png") no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
    }
  }
}
.userInfo {
    height: 200px;
    background: url('@/assets/sc/banner.png');
    background-size: cover;
    .data {
     display: flex;
     justify-content: space-around;
     .data-item {
       height: 45px;
       display: flex;
       align-items: center;
       justify-content: center;
       flex-direction: column;
       color: #fff;
       span:nth-child(1) {
         font-size: 16px;
         margin-bottom: 5px;
       }
       span:nth-child(2) {
         font-size: 12px;
       }
     }
   }
   .nav-grid {
    margin-top: 10px;
     background: #fff;
     .toutiao {
       font-size: 45px;
     }
     .toutiao-shoucang {
       color: #EB5253;
     }
     .toutiao-lishi {
       color: #FF9D1D;
     }
     .text {
       color: #000;
       margin-top: 8px;
       font-size: 28px;
     }
   }
   .logout-cell {
    height: 45px;
    text-align: center;
    color: #EB5253;
    margin: 10px 0;
    font-size: 16px;
   }
    .base {
      height: 140px;
      padding: 0px 5px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        margin-top: 50px;
        display: flex;
        align-items: center;
        .avatar {
          width: 80px;
          height: 80px;
          border: 1.5px solid #fff;
        }
        .name {
          color: #fff;
          font-size: 16px;
          margin-left: 22px;
        }
      }
      .right{
        margin-top: 50px;
        margin-right: 15px;
        .van-button__text{
          font-size: 12px;
        }
      }
    }
}
.not-login{
  position: relative;
  .not-login-span{
    position: absolute;
    top: 36%;
    left: 50%;
    transform: translate(-50%,-50%);
    color: #fff;
  }
  .not-login-img{
    position: absolute;
    top: 21%;
    left: 50%;
    transform: translate(-50%,-50%);
    height: 60px;
  }
}
</style>
